//
//  ATHttpTool.h
//  Postre
//
//  Created by CoderLT on 2017/5/22.
//  Copyright © 2017年 CoderLT. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <YYKit/YYReachability.h>
#import <AFNetworking/AFHTTPSessionManager.h>
#import <AFNetworking/AFURLRequestSerialization.h>

#ifdef DEBUG
#ifndef ATHttpLog
#define ATHttpLog(...) //NSLog(__VA_ARGS__)
#endif
#else
#ifndef ATHttpLog
#define ATHttpLog(...)
#endif
#endif

#define ATHttpToolDefaultTimeoutInterval 30.0f

typedef NS_ENUM(NSInteger, ATHttpMethod) {
    ATHttpMethodGet = 0,
    ATHttpMethodPost,
    ATHttpMethodPut,
    ATHttpMethodDelete,
};

@protocol AFMultipartFormData;

@interface ATHttpTool : NSObject
+ (instancetype)sharedInstance;
+ (BOOL)reachable;
+ (BOOL)reachableWifi;
+ (BOOL)reachableWWAN;
+ (NSString *)networkType;


+ (NSURLSessionDataTask *)GET:(NSString *)urlString
                       params:(id)params
                      success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                      failure:(void (^)(NSString *errorMsg))failure;

+ (NSURLSessionDataTask *)POST:(NSString *)urlString
                        params:(id)params
                       success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                       failure:(void (^)(NSString *errorMsg))failure;

+ (NSURLSessionDataTask *)POST:(NSString *)urlString
                        params:(id)params
                     bodyBlock:(void (^)(id <AFMultipartFormData> formData))block
                       success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                       failure:(void (^)(NSString *errorMsg))failure;

+ (NSURLSessionDataTask *)POST:(NSString *)urlString
                        params:(id)params
                     bodyBlock:(void (^)(id <AFMultipartFormData> formData))block
                      progress:(void (^)(NSProgress *progress))progress
                       success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                       failure:(void (^)(NSString *errorMsg))failure;

+ (NSURLSessionDataTask *)PUT:(NSString *)urlString
                       params:(id)params
                      success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                      failure:(void (^)(NSString *errorMsg))failure;

+ (NSURLSessionDataTask *)DELETE:(NSString *)urlString
                          params:(id)params
                         success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                         failure:(void (^)(NSString *errorMsg))failure;

+ (NSURLSessionDataTask *)method:(ATHttpMethod)method
                       urlString:(NSString *)urlString
                          params:(id)params
                       bodyBlock:(void (^)(id<AFMultipartFormData>))block
                        progress:(void (^)(NSProgress *progress))progress
                         success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                         failure:(void (^)(NSString *errorMsg))failure;

/****************************************************************************
 *  子类可重载该方法, 重写
 *
 *  responseObject 返回json
 *  task           请求task
 *  method         请求方法
 *  urlString      请求URL
 *  params         请求的参数
 *  exParams       附加参数
 *  block          请求body构造函数
 *  progress       请求进度回调
 *  completion     请求回调
 *
 *****************************************************************************/
+ (NSURLSessionDataTask *)manager:(AFHTTPSessionManager *)manager
                           method:(ATHttpMethod)method
                        urlString:(NSString *)urlString
                           params:(id)params
                         exParams:(NSDictionary *)exParams
                        bodyBlock:(void (^)(id<AFMultipartFormData>))block
                         progress:(void (^)(NSProgress *progress))progress
                       completion:(void (^)(NSURLSessionDataTask *task, id responseObject, NSError *error))completion NS_REQUIRES_SUPER;
@end
